package com.hyt.it.ogt.ks.controller;


import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.utils.exception.BusinessException;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.service.ICandidateLogAnswerService;
import com.hyt.it.ogt.ks.service.ICandidateLogOtherService;
import com.hyt.it.ogt.ks.service.es.IEsAndDbService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 考生行为轨迹操作类
 * @author 朱振宇
 * @since 2023-03-13
 */
@Api(tags = "36.考生行为轨迹es接口", value = "考生行为轨迹es接口")
@ApiSort(36)
@Slf4j
@RestController
@RequestMapping("/ks/candidateOperationTrackLog")
public class EsCandidateTrackLogController {

    @Resource
    private ICandidateLogAnswerService candidateLogAnswerService;

    @Resource
    private ICandidateLogOtherService candidateLogOtherService;

    @Resource
    private IEsAndDbService esAndDbService;

    @Resource
    private RedisTemplate redisTemplate;

    @PutMapping("/syncEsByExamId/{examId}")
    @ApiOperation(value = "根据考试ID同步数据到ES", notes = "状态码：20000 成功 异常码：42360101 失败")
    @ApiOperationSort(1)
    public Object syncEsByExamId(@PathVariable String examId) {
        if (!redisTemplate.opsForValue().setIfAbsent("ks:syncEsByExamId" + examId, 1, 10, TimeUnit.HOURS)) {
            throw new BusinessException("根据考试ID同步数据到ES任务进行中...");
        }
        try {
            esAndDbService.deleteCandidateOperationTrackLogByExamId(examId);
            candidateLogAnswerService.syncAnswerByExamId(examId);
            candidateLogOtherService.syncOtherByExamId(examId);
            return ApiResponse.builder()
                    .code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .build();
        } catch (Exception ex) {
            log.error("根据考试ID同步数据到ES异常，收到参数为:【{}】,", examId, ex);
            return ApiResponse.builder()
                    .code(BizStatusCode.SYNC_ES_BY_EXAM_ID.getCode())
                    .build();
        } finally {
            redisTemplate.delete("ks:syncEsByExamId" + examId);
        }
    }

    @PutMapping("/syncEsAll")
    @ApiOperation(value = "同步所有考生轨迹数据到ES", notes = "状态码：20000 成功 异常码：42360101 失败")
    @ApiOperationSort(2)
    public Object syncEsAll() {
        if (!redisTemplate.opsForValue().setIfAbsent("ks:syncEsAll", 1, 10, TimeUnit.HOURS)) {
            throw new BusinessException("同步所有考生轨迹数据到ES任务进行中...");
        }
        try {
            esAndDbService.deleteCandidateOperationTrackLogAll();
            candidateLogAnswerService.syncAnswerAll();
            candidateLogOtherService.syncOtherAll();
            return ApiResponse.builder()
                    .code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .build();
        } catch (Exception ex) {
            log.error("同步所有考生轨迹数据到ES异常,", ex);
            return ApiResponse.builder()
                    .code(BizStatusCode.SYNC_ES_BY_EXAM_ID.getCode())
                    .build();
        } finally {
            redisTemplate.delete("ks:syncEsAll");
        }
    }

}
